const { getAll, add, update, remove } = require('../../utils/dbTools');
const { arrToTree, hasChildNodes, isValidator, createLog } = require('../../utils/util');
// 数据表名称
const tableName = 'menu';
/**
 * 查询菜单列表
 */
const menuList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size, istree } = req.query;
    const params = {};
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    const result = await getAll(tableName, params);
    output.data = istree == 1 ? arrToTree(result) : result;


    res.send(output);
}

/**
 * 新增菜单
 * 
 */
const menuAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { pid, title, url, icon, status } = req.body;
    if (!title || pid === undefined) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    // 检测菜单名称是否可用
    const valid = await isValidator(tableName, 'title', title);
    if (!valid) {
        output.code = 500;
        output.msg = '数据已存在,不能重复添加';
        return res.send(output);
    }

    const data = { pid, title }

    if (url) data.url = url;
    if (icon) data.icon = icon;
    if (status) data.status = status;


    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    output.data = result;
    createLog(req, { type: 8, remark: "新增菜单" });
    res.send(output);
}

/**
 * 更新菜单信息
*/
const menuUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id, pid, title, url, icon, status } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    const data = {}
    if (url) data.url = url;
    if (icon) data.icon = icon;
    if (pid !== '' || pid !== null || pid !== undefined) {
        data.pid = pid;
    }
    if (pid > 0) {
        data.icon = '';
    }
    if (pid == 0) {
        data.url = '';
    }
    if (title) data.title = title;
    if (status) data.status = status;
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    createLog(req, { type: 9, remark: "更新菜单" });
    res.send(output);
}
/**
 * 删除菜单
*/

const menuRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    // 检测待删除节点是否存在子节点, 如果存在, 则不允许删除
    const hasChild = await hasChildNodes(tableName, id);
    if (hasChild) {
        output.code = 500;
        output.msg = '该节点下存在子节点, 请先删除子节点';
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }

    // 记录登录日志
    createLog(req, { type: 2, remark: "删除菜单" })

    res.send(output);
}

module.exports = {
    menuList,
    menuAdd,
    menuUpdate,
    menuRemove
}